package model;

import javax.swing.table.AbstractTableModel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: Daniel
 * Date: 08.08.13
 */

public class WorkerTableModel extends AbstractTableModel {
    private String[] collumnNames = {"Imie", "Nazwisko", "Miasto", "Data urodzenia"};
    private List<Worker> wierszeZPrac;

    public WorkerTableModel() {
        wierszeZPrac = new ArrayList<Worker>();
    }

    public WorkerTableModel(List<Worker> wierszeZPrac) {
        if (wierszeZPrac == null)
            this.wierszeZPrac = new ArrayList<Worker>();
        else
            this.wierszeZPrac = wierszeZPrac;
    }

    public void addRow(Worker worker) {
        wierszeZPrac.add(worker);
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return wierszeZPrac.size();
    }

    @Override
    public int getColumnCount() {
        return collumnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return collumnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return wierszeZPrac.get(rowIndex).getImie();
            case 1:
                return wierszeZPrac.get(rowIndex).getNazwisko();
            case 2:
                return wierszeZPrac.get(rowIndex).getMiasto();
            case 3:
                Date dataUr = wierszeZPrac.get(rowIndex).getDataUr();
                SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
                return df.format(dataUr);
            default:
                return "";
        }
    }

    public void deleteRow(int selectedRow) {
        wierszeZPrac.remove(selectedRow);
        fireTableDataChanged();
    }

    public List<Worker> getWierszeZPrac() {
        return wierszeZPrac;
    }
}
